package com.ruoyi.socket.api;

import com.ruoyi.gams.domain.GamsDevice;
import com.ruoyi.gams.domain.GamsDeviceMedia;
import com.ruoyi.gams.domain.GamsUser;
import com.ruoyi.gams.domain.GamsUserQueue;
import com.ruoyi.gams.service.*;
import com.ruoyi.gams.vo.CardInfoParam;
import com.ruoyi.gams.vo.DeviceLoginParam;
import com.ruoyi.gams.vo.Result;
import com.ruoyi.socket.server.SocketClientStore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "1.设备接口")
@RestController
@RequestMapping("/device-api")
public class DeviceLoginController
{
    @Autowired
    private SocketClientStore socketClientStore;

    @Autowired
    private IGamsUserService userService;

    @Autowired
    private IGamsDeviceService deviceService;

    @Autowired
    private IGamsUserQueueService userQueueService;

    @Autowired
    private IGamsBusinessQueueService businessQueueService;

    @Autowired
    private IGamsDeviceMediaService deviceMediaService;

    @ApiOperation("1.0 登录")
    @ResponseBody
    @PostMapping("/login")
    public Result<GamsDevice> login(@RequestBody DeviceLoginParam param)
    {
        return deviceService.login(param);
    }

    @ApiOperation("1.1 业务列表")
    @ResponseBody
    @PostMapping("/businessList")
    public Result<List<Map<String, Object>>> businessList(Integer deviceId)
    {
        List<Map<String, Object>> list = businessQueueService.findQueueListWithCount(deviceId);
        return Result.success(list);
    }

    @ApiOperation("1.2 轮播图")
    @ResponseBody
    @GetMapping("/bannerList")
    public Result<List<GamsDeviceMedia>> bannerList(Integer deviceId)
    {
        GamsDeviceMedia gamsDeviceMedia = new GamsDeviceMedia();
        gamsDeviceMedia.setStatus(1);
        gamsDeviceMedia.setDeviceId(deviceId);
        List<GamsDeviceMedia> gamsDeviceMedia1 = deviceMediaService.selectGamsDeviceMediaList(gamsDeviceMedia);
        return Result.success(gamsDeviceMedia1);
    }

    @ApiOperation("刷身份证取号")
    @ResponseBody
    @PostMapping("/cardInfo")
    public Result<Boolean> cardInfo(@RequestBody CardInfoParam param)
    {
        //1.添加身份证信息
        GamsUser user = userService.findFirstByCertnumber(param.getCardInfo());
        if (user == null)
        {
            user = new GamsUser();
            user.setCertnumber(param.getCardInfo());
            user.setName("");
            user.setCardId(param.getCardInfo());
            userService.insertGamsUser(user);
        }

        //2.取号
        GamsUserQueue userQueue = userQueueService.getNumber(user, param.getBusinessId());

        //3.打印小票
        socketClientStore.send(param.getDeviceId(), userQueue, user);
        return Result.success();
    }

    @ApiOperation("新增刷身份证记录")
    @ResponseBody
    @PostMapping("/addCardInfo")
    public Result<Boolean> addCardInfo(@RequestBody GamsUser param)
    {
        userService.insertGamsUser(param);
        return Result.success();
    }
}
